/**
 * 用户管理初始化
 */
var User = {
    id: "userTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1,
    deptId:0
};

/**
 * 初始化表格的列
 */
User.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '账号', field: 'account', align: 'center', valign: 'middle', sortable: true},
        {title: '姓名', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '性别', field: 'sexName', align: 'center', valign: 'middle', sortable: true},
        {title: '电话', field: 'phoneNum', align: 'center', valign: 'middle', sortable: true},
        {title: '生日', field: 'birthday', align: 'center', valign: 'middle', sortable: true},
        {title: '角色', field: 'roleName', align: 'center', valign: 'middle', sortable: true},
        {title: '部门', field: 'deptName', align: 'center', valign: 'middle', sortable: true},
        {title: '职务', field: 'postName', align: 'center', valign: 'middle', sortable: true},
        {title: '应用', field: 'appName', align: 'center', valign: 'middle', sortable: true},
        {title: '状态', field: 'statusName', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'gmtCreate', align: 'center', valign: 'middle', sortable: true},
        {title: '更新时间', field: 'gmtModified', align: 'center', valign: 'middle', sortable: true}
    ];
    return columns;
};

/**
 * 检查是否选中
 */
User.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Common.info("请先选中表格中的某一记录！");
        return false;
    }else{
        User.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加用户
 */
User.openAddUser = function () {
    var index = layer.open({
        type: 2,
        title: '添加用户',
        area: ['800px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Common.ctxPath + '/user/to_add'
    });
    this.layerIndex = index;
};

/**
 * 点击编辑用户
 */
User.openEditUser = function () {
    if (this.check()) {
        var userId = this.seItem.id;
        var index = layer.open({
            type: 2,
            title: '编辑用户',
            area: ['800px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Common.ctxPath +'/user/to_update/' + userId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除用户
 */
User.deleteUser = function () {
    if (this.check()) {
        var userId = this.seItem.id;
        var operation = function(){
            var ajax = new $ax(Common.ctxPath + "/user/remove", function (result) {
                if (result.success) {
                    Common.success(result.message + "!");
                    User.table.refresh();
                } else {
                    Common.error(result.message + "!");
                }
            }, function (result) {
                Common.error(result.responseJSON.message + "!");
            });
            ajax.set("userId", userId);
            ajax.start();
        };
        Common.confirm("是否删除用户：" + this.seItem.account + "?",operation);
    }
};

/**
 * 重置密码
 */
User.resetUserPwd = function () {
    if (this.check()) {
        var userId = this.seItem.id;
        parent.layer.confirm('是否重置密码为111111？', {
            btn: ['确定', '取消'],
            shade: false //不显示遮罩
        }, function () {
            var ajax = new $ax(Common.ctxPath + "/user/resetPwd", function (result) {
                if (result.success) {
                    Common.success(result.message + "!");
                    User.table.refresh();
                } else {
                    Common.error(result.message + "!");
                }
            }, function (result) {
                Common.error(result.responseJSON.message + "!");
            });
            ajax.set("userId", userId);
            ajax.start();
        });
    }
};

/**
 * 冻结用户账户
 */
User.freezeUser = function () {
    if (this.check()) {
        var userId = this.seItem.id;
        var ajax = new $ax(Common.ctxPath + "/user/freeze", function (result) {
            if (result.success) {
                Common.success(result.message + "!");
                User.table.refresh();
            } else {
                Common.error(result.message + "!");
            }
        }, function (result) {
            Common.error(result.responseJSON.message + "!");
        });
        ajax.set("userId", userId);
        ajax.start();
    }
};

/**
 * 解除冻结用户账户
 */
User.unfreezeUser = function () {
    if (this.check()) {
        var userId = this.seItem.id;
        var ajax = new $ax(Common.ctxPath + "/user/unfreeze", function (result) {
            if (result.success) {
                Common.success(result.message + "!");
                User.table.refresh();
            } else {
                Common.error(result.message + "!");
            }
        }, function (result) {
            Common.error(result.responseJSON.message + "!");
        });
        ajax.set("userId", userId);
        ajax.start();
    }
}

/**
 * 点击角色分配
 */
User.openEditUserRole = function () {
    if (this.check()) {
        var appName = this.seItem.appName;
        if (appName == "--") {
            Common.error("请先分配用户应用!");
            return;
        }
        var userId = this.seItem.id;
        var index = layer.open({
            type: 2,
            title: '角色分配',
            area: ['300px', '400px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Common.ctxPath + "/user/to_roleAssign/" + userId
        });
        this.layerIndex = index;
    }
};

/**
 * 点击应用分配
 */
User.openEditUserApp = function () {
    if (this.check()) {
        var userId = this.seItem.id;
        var index = layer.open({
            type: 2,
            title: '应用分配',
            area: ['300px', '400px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Common.ctxPath + "/user/to_appAssign/" + userId
        });
        this.layerIndex = index;
    }
};

/**
 * 重置搜索
 */
User.resetSearch = function () {
    $("#condition").val("");
    $("#status").val("");
    User.search();
}

/**
 * 搜索
 */
User.search = function () {
    var queryData = {};
    queryData['deptId'] = User.deptId;
    queryData['condition'] = $("#condition").val();
    queryData['status'] = $("#status").val();
    User.table.refresh({query: queryData});
}

/**
 * 部门选择
 */
User.onClickDept = function (e, treeId, treeNode) {
    User.deptId = treeNode.id;
    User.search();
};

$(function () {
    var defaultColunms = User.initColumn();
    var table = new BSTable(User.id, "/user/list", defaultColunms);
    table.setPaginationType("client");
    User.table = table.init();
    var ztree = new $ZTree("deptTree", "/tree/dept");
    ztree.bindOnClick(User.onClickDept);
    ztree.init();
});
